<template>
    <div>
        <div><h1>登录页面</h1></div>
        <div>
            <div></div>
            <div class="login">
                <el-form :model="ruleForm" :rules="rules" status-icon ref="ruleForm" label-width="100px">
                  <el-form-item label="登录账号" prop="username">
                    <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
                  </el-form-item>
                  
                  <el-form-item label="登录密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                  </el-form-item>

                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                  </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {loginByCloud} from '../api/api.js'
    export default {
        name: 'login',
        data() {                 
            return {
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        { required: true, message: '请输入登录账号', trigger: 'blur' },
                        { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入登录密码', trigger: 'blur' },
                        { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                console.log("submitForm:"+formName)
                
                //
                console.log(this.ruleForm.username)
                console.log(this.ruleForm.password)
                
                //
                this.$refs[formName].validate((valid) => {
                  if (valid) {
                    // 进行登录
                    // this.handleLogin(this.ruleForm.username, this.ruleForm.password)
                    this.handleLoginByJson(this.ruleForm.username, this.ruleForm.password)
                  } else {
                    console.log('error submit!!');
                    return false;
                  }
                });
            },
            resetForm(formName) {
                console.log("resetForm:"+formName)
                this.$refs[formName].resetFields();
            },
            
            // @RequestParam : FormData()
            handleLogin(username, password) {
                
                let data = new FormData();
                data.append('username',username);
                data.append('password',password);
                
                this.$http.post('/user/login', data)
                  .then(function (response) {
                    console.log(response);
                  })
                  .catch(function (error) {
                    console.log(error);
                  });
            },
            
            // 
            handleLoginByJson(username, password) {
                
                let data = {}
                data.name = username
                data.pass = password
                
                loginByCloud(data)
                .then(res => {
                    if(res.data.code === 200) {
                        console.log("msg",res.data.msg);
                        console.log("userName",res.data.data.userName);
                        let userInfo = JSON.stringify(res.data.data)
                        window.sessionStorage.setItem('userInfo', userInfo)
                        
                        // 单独存储token
                        let token = res.data.data.accessToken
                        window.sessionStorage.setItem('token', token)
                        
                        this.$router.push('/userlist')
                    }
                    else {
                        let msg = res.data.msg
                        this.$message.error(msg);
                    }

                })
                .catch(err => {
                    console.log(err)
                })
            }
        }
    }
</script>

<style scoped>
    .login {
        width: 400px;
        height: 200px;
        margin: auto;
    }
</style>
